//
//  WBTabBar.m
//  1229-Weibo.
//
//  Created by 千锋 on 15/12/29.
//  Copyright (c) 2015年 千锋. All rights reserved.
//

#import "WBTabBar.h"
#import "WBTabBarButton.h"


@interface WBTabBar ()

/** 按钮数组*/
@property(nonatomic,strong)NSMutableArray * buttonArray;
/** 加号按钮*/
@property(nonatomic,strong)UIButton * plusBtn;



@end

@implementation WBTabBar
{
    WBTabBarButton * _selectedBtn;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        //创建一个按钮 设置一次性的属性
        self.plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:self.plusBtn];
        [self.plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [self.plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [self.plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [self.plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    }
    return self;
}

/** 懒加载*/
-(NSMutableArray *)buttonArray{
    if (_buttonArray == nil) {
        _buttonArray = @[].mutableCopy;
    }
    return _buttonArray;
}

-(void)setTabBarItem:(UITabBarItem *)tabBarItem{
    //获取到 标题 title ,图片 image 等
    NSLog(@"title:%@",tabBarItem.title);
    
    //每一次进来就创建一个button
    WBTabBarButton * btn = [WBTabBarButton buttonWithType:UIButtonTypeCustom];
    
    //给btn 传入当前的tabBarItem值
    btn.tabBarItem = tabBarItem;
    
    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    //添加到view 中
    [self addSubview:btn];
    //存储在全局数组里面
    [self.buttonArray addObject:btn];
    //当第一次调用这个方法，就让第一个处于选中
    if (self.buttonArray.count == 1) {
        [self buttonPressed:btn];
    }
    
    //修改图标 和 标题的偏移量
    //btn.imageEdgeInsets = UIEdgeInsetsMake(-30, 0, 0, 0);
    //btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}

//buttonClicked:@selector

-(void)buttonPressed:(WBTabBarButton *)sender{
    
    //1.循环
    
    //    for (WBTabBarButton * btn2  in self.buttonArray) {
    //        btn2.selected = NO;
    //    }
    //    sender.selected = YES;
    //2.记录的方式
    _selectedBtn.selected = NO;
    
    sender.selected = YES;
    
    _selectedBtn = sender;
    
    NSInteger index = [self.buttonArray indexOfObject:sender];
    
    //响应代理的方法
    //    if ([_delegate respondsToSelector:@selector(passIndex:)]) {
    //        [_delegate passIndex:index];
    //    }
    //执行block 的方法
    if (_block) {
        _block(index);
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //布局子视图
    //循环按钮数组，布局响应的按钮
    //获取当前宽度
    CGFloat btnW = CGRectGetWidth(self.frame) / (CGFloat)(self.buttonArray.count +1);
    //设置plusBtn 的高宽
    CGFloat plusW = btnW;
    CGFloat plusH = CGRectGetHeight(self.frame);
    CGFloat plusX = 2 * btnW ;
    self.plusBtn.frame = CGRectMake(plusX, 0, plusW, plusH);
    //遍历按钮数组，设置所有的按钮的frame
    for (int index = 0; index < self.buttonArray.count; index ++) {
        WBTabBar * btn = self.buttonArray[index];
        CGFloat bX = btnW * index;
        if (index > 1) {
            bX += btnW;
        }
        CGFloat bY = 0;
        CGFloat bW = btnW;
        CGFloat bH = CGRectGetHeight(self.frame);
        btn.frame = CGRectMake(bX, bY, bW, bH);
    }
}

@end
